<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Recordset Filter Plugin</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Recordset Filter Plugin</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>The RecordsetFilter plugin provides the ability to filter records within a specific Recordset instance.</p>
</div>

<div class="example yui3-skin-sam">
<div id="demo">
	<table>
		<thead>
			<tr>
				<th>State</th>
				<th>Population</th>
				<th>Land Area</th>
			</tr>
		<tbody>
		</tbody>
	</table>
</div>

<div id="buttonControl">
<input type="button" id="keyValButton" href="filter-key.php" value="Filter out California">
<input type="button" id="functionButton" href="filter-func.php" value="Custom Filter Function (all M-P states)">
<input type="button" id="resetButton" href="reset.php" value="Show Original Recordset">
</div>
<script>
YUI().use("recordset-base", "recordset-filter","node", function (Y) {
	
	var state_census_data = [
        {ANSI: "01000", STATE: "ALABAMA", TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},

        {ANSI: "06000", STATE: "CALIFORNIA", TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},

        {ANSI: "10000", STATE: "DELAWARE", TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401},
        {ANSI: "12000", STATE: "FLORIDA", TOTAL_POP: 18537969, LAND_AREA: 53926.82, POP_PER_SQ_MILE: 296.4},
        {ANSI: "13000", STATE: "GEORGIA", TOTAL_POP: 9829211, LAND_AREA: 57906.14, POP_PER_SQ_MILE: 141.4},
        {ANSI: "15000", STATE: "HAWAII", TOTAL_POP: 1295178, LAND_AREA: 6422.62, POP_PER_SQ_MILE: 188.6},
        {ANSI: "16000", STATE: "IDAHO", TOTAL_POP: 1545801, LAND_AREA: 82747.21, POP_PER_SQ_MILE: 15.6},

        {ANSI: "20000", STATE: "KANSAS", TOTAL_POP: 2818747, LAND_AREA: 81814.88, POP_PER_SQ_MILE: 32.9},
        {ANSI: "21000", STATE: "KENTUCKY", TOTAL_POP: 4314113, LAND_AREA: 39728.18, POP_PER_SQ_MILE: 101.7},
        {ANSI: "22000", STATE: "LOUISIANA", TOTAL_POP: 4492076, LAND_AREA: 43561.85, POP_PER_SQ_MILE: 102.6},

        {ANSI: "26000", STATE: "MICHIGAN", TOTAL_POP: 9969727, LAND_AREA: 56803.82, POP_PER_SQ_MILE: 175},

        {ANSI: "29000", STATE: "MISSOURI", TOTAL_POP: 5987580, LAND_AREA: 68885.93, POP_PER_SQ_MILE: 81.2},
        {ANSI: "30000", STATE: "MONTANA", TOTAL_POP: 974989, LAND_AREA: 145552.43, POP_PER_SQ_MILE: 6.2},
        {ANSI: "31000", STATE: "NEBRASKA", TOTAL_POP: 1796619, LAND_AREA: 76872.41, POP_PER_SQ_MILE: 22.3},

        {ANSI: "34000", STATE: "NEW JERSEY", TOTAL_POP: 8707739, LAND_AREA: 7417.34, POP_PER_SQ_MILE: 1134.5},

        {ANSI: "38000", STATE: "NORTH DAKOTA", TOTAL_POP: 646844, LAND_AREA: 68975.93, POP_PER_SQ_MILE: 9.3},

        {ANSI: "41000", STATE: "OREGON", TOTAL_POP: 3825657, LAND_AREA: 95996.79, POP_PER_SQ_MILE: 35.6},

        {ANSI: "45000", STATE: "SOUTH CAROLINA", TOTAL_POP: 4561242, LAND_AREA: 30109.47, POP_PER_SQ_MILE: 133.2},
        {ANSI: "46000", STATE: "SOUTH DAKOTA", TOTAL_POP: 812383, LAND_AREA: 75884.64, POP_PER_SQ_MILE: 9.9},
        {ANSI: "47000", STATE: "TENNESSEE", TOTAL_POP: 6296254, LAND_AREA: 41217.12, POP_PER_SQ_MILE: 138},
        {ANSI: "48000", STATE: "TEXAS", TOTAL_POP: 24782302, LAND_AREA: 261797.12, POP_PER_SQ_MILE: 79.6},

        {ANSI: "56000", STATE: "WYOMING", TOTAL_POP: 544270, LAND_AREA: 97100.4, POP_PER_SQ_MILE: 5.1	}
    ],
	TEMPLATE_TR = '<tr id="row_{ANSI}"><td>{STATE}</td><td>{TOTAL_POP}</td><td>{LAND_AREA}</td></tr>';
	
  	// Create a Recordset instance.
  	var recordset = new Y.Recordset({records:state_census_data});
	recordset.plug(Y.Plugin.RecordsetFilter);
	
	
	renderRows(recordset);
	setUpButtons(recordset);

	function setUpButtons(rs) {

		function getRandomInt(min, max)
		{
		  return Math.floor(Math.random() * (max - min + 1)) + min;
		}
		
		Y.one('#keyValButton').on('click', function(e) {
			//prevent default behavior (stop link)
			e.preventDefault();
			var sub_recordset = rs.filter.filter('STATE',"CALIFORNIA");
			renderRows(sub_recordset);
		});
		
		Y.one('#functionButton').on('click', function(e) {
			e.preventDefault();
			
			var customFunc = function(item) {
				var letter = item.getValue('STATE').charAt(0);
				if (letter === 'M' || letter === 'N' || letter === 'O' || letter === 'P') {
					return true;
				}
				else {
					return false;
				}
			},
			
			sub_recordset = rs.filter.filter(customFunc);
			renderRows(sub_recordset);
		});
		
		Y.one('#resetButton').on('click', function(e) {
			e.preventDefault();
			renderRows(recordset);
		});

	}
	
	function renderRows(rs) {
		var tbody = Y.one('#demo tbody'),
		i=0,
		len = rs.getLength(),
		html = '';
		
		if (tbody.hasChildNodes()) {
			tbody.get('children').remove();
		}
		
		for (; i < len; i++) {
			html += Y.Lang.sub(TEMPLATE_TR, rs.getRecord(i).getValue());
		}
		tbody.append(html);
	}
});
</script>

</div>

<p>The filter plugin allows operations to filter a Recordset instance. All the methods generate new Recordsets that are subsets of the original Recordset.</p>

<h3>Using the Plugin</h3>

<ul class="topspace">
	<li><a href="#keyvalue">Filter by key/value</a></li>
	<li><a href="#customFunction">Using custom filter functions</a></li>
	<li><a href="#other">Other ways to filter</a></li>
</ul>

<p>The RecordsetFilter plugin supports filtering a Recordset instance using a custom filter function, or key/value pairs.</p>

<pre class="code prettyprint">YUI().use(&quot;recordset-base&quot;, &quot;recordset-filter&quot;, function(Y) {
    var state_census_data = [
        {ANSI: &quot;01000&quot;, STATE: &quot;ALABAMA&quot;, TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
        {ANSI: &quot;06000&quot;, STATE: &quot;CALIFORNIA&quot;, TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
        {ANSI: &quot;10000&quot;, STATE: &quot;DELAWARE&quot;, TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401}
    ],
	
	&#x2F;&#x2F;instantiate a new Recordset
	recordset = new Y.Recordset({records: state_census_data});
	
	&#x2F;&#x2F;Plug it with the Filter plugin
	recordset.plug(Y.Plugin.RecordsetFilter);
	
	&#x2F;&#x2F;call filter methods with the &#x27;filter&#x27; namespace
	var subset = recordset.filter.filter(&quot;STATE&quot;, &quot;CALIFORNIA&quot;);
});</pre>



<h3 id="keyvalue">Filtering by Key/Value</h3>

<p>The filter() method can take a key as its first argument, and a value as its second. Doing so will return a subset of the original Recordset, with records that match the specific key/value pair.</p>

<pre class="code prettyprint">&#x2F;&#x2F;call filter methods with the &#x27;filter&#x27; namespace
var subset = recordset.filter.filter(&quot;STATE&quot;, &quot;CALIFORNIA&quot;);</pre>



<h3 id="customFunction">Filtering by Custom Function</h3>

<p>Additionally, a custom function can be passed in to the filter method. In this scenario, all records will be evaluated against this function, and if a truthy value is returned, it will be pushed onto the returning sub-Recordset.</p>

<pre class="code prettyprint">var myCustomFilterFunction = function(item) {
	var letter = item.getValue(&#x27;STATE&#x27;).charAt(0);
	if (letter === &#x27;M&#x27; || letter === &#x27;N&#x27; || letter === &#x27;O&#x27; || letter === &#x27;P&#x27;) {
		return true;
	}
	else {
		return false;
	}
};

&#x2F;&#x2F;All records which have a STATE property starting with &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, or &quot;P&quot; will be pushed onto the new Recordset.
var subset = recordset.filter.filter(myCustomFilterFunction);</pre>


<h3 id="other">Other Ways to Filter</h3>

<p>As Recordset augments <code>Y.Arraylist</code>, methods from the <code>array-extras</code> sub-module can be used. Currently, the <code>RecordsetFilter</code> plugin supports <code>reject()</code> and <code>grep()</code>. However, using other methods from the <code>array-extras</code> utility is straight-forward:</p>

<pre class="code prettyprint">&#x2F;&#x2F;use the &#x27;array-extras&#x27; submodule
YUI().use(&quot;recordset-base&quot;, &quot;recordset-filter&quot;, &quot;array-extras&quot;, function(Y) {
	
    var state_census_data = [
        {ANSI: &quot;01000&quot;, STATE: &quot;ALABAMA&quot;, TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
        {ANSI: &quot;06000&quot;, STATE: &quot;CALIFORNIA&quot;, TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
        {ANSI: &quot;10000&quot;, STATE: &quot;DELAWARE&quot;, TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401}
	],
	&#x2F;&#x2F;instantiate a new Recordset
	recordset = new Y.Recordset({records: state_census_data});
	
	&#x2F;&#x2F;Define a custom function
	customFindFunction = function() {
		...
	}
	
	&#x2F;&#x2F;Use the Array.find method found in the array-extras sub-module
	var subset = new Y.Recordset({
	       records: Y.Array.find(recordset.get(&#x27;records&#x27;), customFindFunction)
	});
});</pre>


</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Shows how to instantiate and interact with a standard Recordset by adding, removing and updating records/objects. Explains the events that are fired, and shows how to access the built-in hash table that stores records by their YUIDs.">
                                        <a href="recordset-basic.html">Basic Recordset</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to sort a Recordset using the default sorter, or a custom sort function. Additionally, demonstrates how convenience attributes help in determining when Recordset is in a sorted state, and what the last sort parameters were.">
                                        <a href="recordset-sort.html">Recordset Sort Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use filtering methods to Recordset. Key-value filtering, and filtering by providing a custom function are covered.">
                                        <a href="recordset-filter.html">Recordset Filter Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to create and access hash tables on Recordset. Tables are kept in sync through events fired by Recordset.">
                                        <a href="recordset-indexer.html">Recordset Indexer Plugin</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
